<?php
require_once('TVB/pms/pdes/action/InfoGetAction.class.php');

class InfoAwardUpdateAction extends BaseAction 
{
	public $id;
    
    /**
	 * @maxLength 255
	 * @label Award Title (Chinese)
	 */
	public $award_title_chinese;

    /**
	 * @maxLength 255
	 * @label Award Title (English)
	 */
	public $award_title_english;

    /**
	 * @maxLength 255
	 * @label Award Type
	 */
	public $award_type;
    
    /**
	 * @filter positive_number
	 * @maxLength 11
	 * @label Year
	 */
	public $year;

    /**
	 * @filter datetime
	 * @label Publish Date/Time 
	 */
    public $publish_datetime;
    
    /**
	 * @filter datetime
	 * @label Expiry Date/Time
	 */
	public $expire_datetime;

	public $winners;
    public $description;
	public $award_presenters;
	public $nominees;
	public $organizations;
	public $status;
	public $created_by;
	public $created_at;
	public $modified_by;
	public $modified_at;
	public $episode_id;

	public $programme_id;
	public $onair_episode_no;

	#public $role_id;
	public $cast_id; # modify by vanni,for bug 7397

	public $name_type;
	public $name_last;
	public $name_first;
	public $alphabet;

	public $ut;
	public $reset;
	public $from;
	public function execute(){

		if (!is_null($this->reset))
		{
			$this->setData('id', $this->id);
			$this->setData('type', 'award');
			$this->setData('act', 'edit');
			$this->setData('from', $this->from);
			return 'reset';
		}

		Model::$dbo['w']->beginTransaction();
		try
		{
			if (CommonUtils::is_concurrent('info_award', $this->id, $this->modified_at))
			{
				$this->init_page_data();
				$this->addError('concurrent', array('0105', 'Concurrent update found!'));
				return 'logic-error';
			}

			if(count($this->_errors) > 0){
				$this->init_page_data();
				return 'filter-error';
			}

			//check name unique
			if (is_null($this->award_title_chinese) && is_null($this->award_title_english))
			{
				$this->init_page_data();
				$this->addError('Award.Award Title', array('0042', 'Award Title'));
				return 'filter-error';
			}
  
            if (!is_null($this->expire_datetime))
			{
				if ($this->publish_datetime > $this->expire_datetime)
				{
					$this->init_page_data();
					$this->addError('Publish Date/Time && Expiry Date/Time', array('0033', 'Publish Date/Time && Expiry Date/Time'));
					return 'filter-error';
				}
			}

			$update_id = $this->update_info_award();
			if ($update_id)
			{
				$this->setData('type','award');
				$this->setData('onair_episode_no', $this->onair_episode_no);
				$this->setData('programme_id', $this->programme_id);
			}

			Model::$dbo['w']->commit();

			return ($this->from == 'task') ? 'task' : 'succ';
		}
		catch(Exception $e)
		{
			error_log("Update Award Fails: " . var_export($this, 1));
			error_log("Exception: " . var_export($e, 1));

			Model::$dbo['w']->rollBack();

			return 'system-error';
		}
	}    

	/**
	 * update info_award
	 */
	private function update_info_award()
	{
		$info_award = new info_award();
		$info_award = $info_award->get($this->id);
		Model::diff($info_award, $this);
		$info_award->modified_at = date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']);
		$info_award->modified_by = CommonUtils::get_user();
		$info_award->winners = CommonUtils::json_info_data($this, 'winners');
		$info_award->award_presenters = CommonUtils::json_info_data($this, 'award_presenters');
		$info_award->nominees = CommonUtils::json_info_data($this, 'nominees');
		$info_award->organizations = CommonUtils::json_info_data($this, 'organizations');
		$info_award->award_title_chinese = trim($this->award_title_chinese);
		$info_award->award_title_english = trim($this->award_title_english);
		$info_award->award_type = trim($this->award_type);
        $info_award->publish_datetime = $this->publish_datetime;
        $info_award->expire_datetime  = $this->expire_datetime;
        $update_id = Model::$dbo['w']->save($info_award);
		if ($update_id > 0)
		{
            $primary_key = array(
                'id'    => $this->id
            );
            $fields = array(
                'award_title_chinese',
                'award_title_english',
                'award_type',
                'winners',
                'description',
                'award_presenters',
                'nominees',
                'organizations'
            );
            CommonUtils::filterC2A0('info_award', $primary_key, $fields);

			$info_award->add_change_log();

			$this->addInfo("info_award:".AC::LOG_OP_UPDATE.":".$this->id, "update info_award success", json_encode($info_award));
			return true;
		}
		else
		{
			$this->addError("info_award:".AC::LOG_OP_UPDATE.":".$this->id, "update info_award failed", json_encode($info_award));
			return false;
		}
	}

	/**
	 * init page data
	 */
	private function init_page_data()
	{
		$action = new InfoGetAction();
		$action->id = $this->id;
		$action->act = 'edit';
		$action->type = 'award';
		$action->execute();

		$old_data = $action->getData();
		$this->modified_at = $old_data['award']->modified_at;
		$old_data['award'] = Model::diff($old_data['award'], $this);
		foreach ($old_data as $object=>$data)
		{
			$this->setData($object, $data);
		}

		$json_fields = AC::$infoJson['info_award'];
		$json_data = array();
		foreach ($json_fields as $field)
		{
			$json_decode_data = json_decode(CommonUtils::json_info_data($this, $field), true);
			$json_data[$field] = isset($json_decode_data[$field]) ? $json_decode_data[$field] : array();;
		}
		$this->setData("award_json_list", $json_data);

		//jump page
		$this->setData('id', $this->id);
		$this->setData('type', 'award');
		$this->setData('act', 'edit');
		$this->setData('from', $this->from);
	}
}
?>
